<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="名称/商户号" prop="pay_name">
                    <el-input class="w-[280px]" v-model="queryParams.pay_name" clearable placeholder="名称/商户号" />
                </el-form-item>


                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="支付名称" prop="pay_name" show-overflow-tooltip />
                    <el-table-column label="商户号" prop="pay_name">
                        <template #default="{ row }">
                            <span>{{
                                row.pay_method == 1 ? row.wx_mchid :
                                    row.pay_method == 2 ? row.ali_appid :
                                        ''
                            }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="支付方式" prop="pay_method" show-overflow-tooltip>
                        <template #default="{ row }">
                            <span>{{
                                row.pay_method == 1 ? '微信支付' :
                                    row.pay_method == 2 ? '支付宝支付' :
                                        '无支付方式'
                            }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button type="danger" link @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup>
import { apiPayTemplateLists, apiPayTemplateDelete } from '@/api/cloudupdate'
import { usePaging } from '@/hooks/usePaging'
import { PayTypeEnumMap } from '@/enums/appEnums'
import feedback from '@/utils/feedback'
// 查询条件
const queryParams = reactive({
    pay_name: '',
    pay_method: '',
    create_time_start: '',
    create_time_end: ''
})
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiPayTemplateLists,
    params: queryParams
})
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
import EditPopup from './paytem_edit.vue'
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {

    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiPayTemplateDelete({ id })
    getLists()
}

getLists()
</script>